package GUI;
import CTRLayer.*;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JCheckBox;
import javax.swing.JList;
import javax.swing.AbstractListModel;
import javax.swing.ListSelectionModel;

import java.awt.event.ActionEvent;

import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;

public class AddProduct extends JFrame {

	private JPanel contentPane;
	private JTextField txtID;
	private JTextField txtName;
	private JTextField txtSalesPrice;
	private JTextField txtCofO;
	private JTextField txtSupp;
	private JButton btnAddNew;
	private JButton btnShowInformation;
	private JButton btnDelete;
	private JButton btnBackToMain;
	private JLabel lblSize;
	private JLabel lblColour;
	private JTextField txtColour;
	private JLabel lblType;
	private JTextField txtType;
	private JLabel lblFabric;
	private JTextField txtFabric;
	private JLabel lblCalibre;
	private JTextField txtCalibre;
	private JTextField txtPurchasePrice;
	private JTextField txtRentPrice;
	private JTextField txtMinStock;
	private Controller ctr;
	private JComboBox comboBox;
	private JTextField txtQuantity;
	private String size;
	
	private boolean errorPp; private boolean errorSp; private boolean errorRp;
	
	/**
	 * Create the frame.
	 */
	public AddProduct(final String what) {
		setResizable(false);
		
		ctr = new Controller();
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 500, 280);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		setTitle("Add product");
		contentPane.setLayout(null);
		
		JLabel lblId = new JLabel("ID:");
		lblId.setBounds(10, 11, 100, 14);
		contentPane.add(lblId);
		
		JLabel lblName = new JLabel("Name:");
		lblName.setBounds(10, 36, 100, 14);
		contentPane.add(lblName);
		
		JLabel lblSalesPrice = new JLabel("Sales price:");
		lblSalesPrice.setBounds(10, 91, 100, 14);
		contentPane.add(lblSalesPrice);
		
		JLabel lblCountryOfOrigin = new JLabel("Country of origin:");
		lblCountryOfOrigin.setBounds(10, 144, 100, 14);
		contentPane.add(lblCountryOfOrigin);
		
		JLabel lblSupplier = new JLabel("Supplier ID:");
		lblSupplier.setBounds(10, 172, 100, 14);
		contentPane.add(lblSupplier);
		
		txtID = new JTextField();
		txtID.setBounds(120, 8, 86, 20);
		contentPane.add(txtID);
		txtID.setColumns(10);
		
		txtName = new JTextField();
		txtName.setBounds(120, 33, 86, 20);
		contentPane.add(txtName);
		txtName.setColumns(10);
		
		txtSalesPrice = new JTextField();
		txtSalesPrice.setBounds(120, 88, 86, 20);
		contentPane.add(txtSalesPrice);
		txtSalesPrice.setColumns(10);
		
		txtCofO = new JTextField();
		txtCofO.setBounds(120, 141, 86, 20);
		contentPane.add(txtCofO);
		txtCofO.setColumns(10);
		
		txtSupp = new JTextField();
		txtSupp.setBounds(120, 169, 86, 20);
		contentPane.add(txtSupp);
		txtSupp.setColumns(10);
		
		JLabel lblPurchasePrice = new JLabel("Purchase price:");
		lblPurchasePrice.setBounds(10, 64, 100, 14);
		contentPane.add(lblPurchasePrice);
		
		txtPurchasePrice = new JTextField();
		txtPurchasePrice.setBounds(120, 61, 86, 20);
		contentPane.add(txtPurchasePrice);
		txtPurchasePrice.setColumns(10);
		
		JLabel lblRentPrice = new JLabel("Rent price:");
		lblRentPrice.setBounds(10, 116, 100, 14);
		contentPane.add(lblRentPrice);
		
		txtRentPrice = new JTextField();
		txtRentPrice.setBounds(120, 113, 86, 20);
		contentPane.add(txtRentPrice);
		txtRentPrice.setColumns(10);
		
		JLabel lblMinimumStock = new JLabel("Minimum stock:");
		lblMinimumStock.setBounds(234, 94, 100, 14);
		contentPane.add(lblMinimumStock);
		
		txtMinStock = new JTextField();
		txtMinStock.setBounds(344, 91, 86, 20);
		contentPane.add(txtMinStock);
		txtMinStock.setColumns(10);
		
		JButton btnAddSupplier = new JButton("Add supplier");
		btnAddSupplier.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			}
		});
		btnAddSupplier.setBounds(220, 168, 100, 23);
		contentPane.add(btnAddSupplier);
		
		JButton btnAddProduct = new JButton("Add product");
		btnAddProduct.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				switch(what)
				{
				case "c": 
					int SelRowIndex = comboBox.getSelectedIndex();
					switch(SelRowIndex)
					{
					case 0: size = "XS";break;
					case 1: size = "S";break;
					case 2: size = "M";break;
					case 3: size = "L";break;
					case 4: size = "XL";break;
					case 5: size = "XXL";break;
					}
					addCToDB(txtID.getText(), txtName.getText(), txtPurchasePrice.getText(), txtSalesPrice.getText(), txtRentPrice.getText(), txtMinStock.getText(), txtQuantity.getText(), txtCofO.getText(), txtSupp.getText(), size, txtColour.getText());
					break;
				case "e":
					addEToDB(txtID.getText(), txtName.getText(), txtPurchasePrice.getText(), txtSalesPrice.getText(), txtRentPrice.getText(), txtMinStock.getText(), txtQuantity.getText(), txtCofO.getText(), txtSupp.getText(), txtType.getText());
					break;
				case "g":
					addGToDB(txtID.getText(), txtName.getText(), txtPurchasePrice.getText(), txtSalesPrice.getText(), txtRentPrice.getText(), txtMinStock.getText(), txtQuantity.getText(), txtCofO.getText(), txtSupp.getText(), txtFabric.getText(), txtCalibre.getText());
					break;
				}
				
			}
		});
		btnAddProduct.setBounds(202, 200, 118, 23);
		contentPane.add(btnAddProduct);
		
		JButton btnBackToPrevious = new JButton("Back to previous");
		btnBackToPrevious.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				dispose();
			}
		});
		btnBackToPrevious.setBounds(10, 200, 118, 23);
		contentPane.add(btnBackToPrevious);	
		
		txtQuantity = new JTextField();
		txtQuantity.setColumns(10);
		txtQuantity.setBounds(344, 119, 86, 20);
		contentPane.add(txtQuantity);
		
		JLabel lblquantity = new JLabel("Quantity");
		lblquantity.setBounds(234, 122, 100, 14);
		contentPane.add(lblquantity);

		
		details(what);
	}
	


	private void details(String what)
	{
		switch(what){
			case "c": 
				lblSize = new JLabel("Size:");
				lblSize.setBounds(346, 11, 46, 14);
				contentPane.add(lblSize);
				
				comboBox = new JComboBox<String>();
				comboBox.setModel(new DefaultComboBoxModel<String>(new String[] {"XS", "S", "M", "L", "XL", "XXL"}));
				comboBox.setBounds(382, 8, 46, 20);
				contentPane.add(comboBox);
				
				lblColour = new JLabel("Colour:");
				lblColour.setBounds(319, 36, 46, 14);
				contentPane.add(lblColour);
				
				txtColour = new JTextField();
				txtColour.setBounds(363, 33, 65, 20);
				contentPane.add(txtColour);
				txtColour.setColumns(10);
				break;
			case "e": 
				lblType = new JLabel("Type:");
				lblType.setBounds(319, 11, 46, 14);
				contentPane.add(lblType);
				
				txtType = new JTextField();
				txtType.setBounds(353, 8, 75, 20);
				contentPane.add(txtType);
				txtType.setColumns(10);
				break;
			case "g" : 
				lblFabric = new JLabel("Fabric:");
				lblFabric.setBounds(319, 11, 46, 14);
				contentPane.add(lblFabric);
				
				txtFabric = new JTextField();
				txtFabric.setBounds(362, 8, 66, 20);
				contentPane.add(txtFabric);
				txtFabric.setColumns(10);
				
				lblCalibre = new JLabel("Calibre:");
				lblCalibre.setBounds(319, 33, 46, 14);
				contentPane.add(lblCalibre);
				
				txtCalibre = new JTextField();
				txtCalibre.setBounds(362, 33, 66, 20);
				contentPane.add(txtCalibre);
				txtCalibre.setColumns(10);
				break;
		}
	}
	
	private void addCToDB(String id, String name, String purchP, String salesP, String rentP, String minStock, String quantity, String cOfO, String suppID, String size, String colour)
	{
		{
			if(id.isEmpty() || name.isEmpty() || purchP.isEmpty() || salesP.isEmpty() || rentP.isEmpty() || minStock.isEmpty() || quantity.isEmpty() || cOfO.isEmpty() || suppID.isEmpty() || size.isEmpty() || colour.isEmpty())
			{
				JOptionPane.showMessageDialog(null,
					    "All fields have to be filled.",
					    "Warning",
					    JOptionPane.WARNING_MESSAGE);
			}
			else
			{	
				errorPp=false;
				try
				{
					Integer.valueOf(purchP);
				}
				catch(Exception e)
				{
					JOptionPane.showMessageDialog(null,
							"The purchase price should be a number!",
							"Warning",
							JOptionPane.WARNING_MESSAGE);
					errorPp=true;
				}

				if(errorPp!=true)
				{
					errorSp=false;
					try
					{
						Integer.valueOf(salesP);
					}
					catch(Exception e)
					{
						JOptionPane.showMessageDialog(null,
								"The sales price should be a number!",
								"Warning",
								JOptionPane.WARNING_MESSAGE);
						errorSp=true;
					}

					if(errorSp!=true)
					{	
						errorRp=false;
						try
						{
							Integer.valueOf(rentP);
						}
						catch(Exception e)
						{
							JOptionPane.showMessageDialog(null,
									"The rent price should be a number!",
									"Warning",
									JOptionPane.WARNING_MESSAGE);
							errorRp=true;
						}
						
						if(errorRp!=true)
						{
							if((ctr.getProd(name)))
							{
								JOptionPane.showMessageDialog(null,
										"Supplier with the same name already exists.",
										"Warning",
										JOptionPane.WARNING_MESSAGE);
							}
							else
							{
								ctr.newCloth(id, name, Double.valueOf(purchP), Double.valueOf(salesP), Double.valueOf(rentP), Integer.valueOf(minStock), Integer.valueOf(quantity), cOfO, suppID, size, colour);
								
								//table at customer window.addElement(/*???*/);
							
								dispose();
							
							}
						}
					}
				}
			}
		}
	}	
	
	private void addGToDB(String id, String name, String purchP, String salesP, String rentP, String minStock, String quantity, String cOfO, String suppID, String fabFabric, String calibre)
	{
		{
			if(id.isEmpty() || name.isEmpty() || purchP.isEmpty() || salesP.isEmpty() || rentP.isEmpty() || minStock.isEmpty() || quantity.isEmpty() || cOfO.isEmpty() || suppID.isEmpty() || fabFabric.isEmpty()|| calibre.isEmpty())
			{
				JOptionPane.showMessageDialog(null,
					    "All fields have to be filled.",
					    "Warning",
					    JOptionPane.WARNING_MESSAGE);
			}
			else
			{	
				errorPp=false;
				try
				{
					Integer.valueOf(purchP);
				}
				catch(Exception e)
				{
					JOptionPane.showMessageDialog(null,
							"The purchase price should be a number!",
							"Warning",
							JOptionPane.WARNING_MESSAGE);
					errorPp=true;
				}

				if(errorPp!=true)
				{
					errorSp=false;
					try
					{
						Integer.valueOf(salesP);
					}
					catch(Exception e)
					{
						JOptionPane.showMessageDialog(null,
								"The sales price should be a number!",
								"Warning",
								JOptionPane.WARNING_MESSAGE);
						errorSp=true;
					}

					if(errorSp!=true)
					{	
						errorRp=false;
						try
						{
							Integer.valueOf(rentP);
						}
						catch(Exception e)
						{
							JOptionPane.showMessageDialog(null,
									"The rent price should be a number!",
									"Warning",
									JOptionPane.WARNING_MESSAGE);
							errorRp=true;
						}
						
						if(errorRp!=true)
						{
							if((ctr.getProd(id)))
							{
								JOptionPane.showMessageDialog(null,
										"Supplier with the same name already exists.",
										"Warning",
										JOptionPane.WARNING_MESSAGE);
							}
							else
							{
								ctr.newGun(id, name, Double.valueOf(purchP), Double.valueOf(salesP), Double.valueOf(rentP), Integer.valueOf(minStock), Integer.valueOf(quantity), cOfO, suppID, fabFabric, calibre);
								
								//table at customer window.addElement(/*???*/);
							
								dispose();
							
							}
						}
					}
				}
			}
		}
	}
		
		private void addEToDB(String id, String name, String purchP, String salesP, String rentP, String minStock, String quantity, String cOfO, String suppID, String type)
		{
			{
				if(id.isEmpty() || name.isEmpty() || purchP.isEmpty() || salesP.isEmpty() || rentP.isEmpty() || minStock.isEmpty() || quantity.isEmpty() || cOfO.isEmpty() || suppID.isEmpty() || type.isEmpty())
				{
					JOptionPane.showMessageDialog(null,
						    "All fields have to be filled.",
						    "Warning",
						    JOptionPane.WARNING_MESSAGE);
				}
				else
				{	
					errorPp=false;
					try
					{
						Integer.valueOf(purchP);
					}
					catch(Exception e)
					{
						JOptionPane.showMessageDialog(null,
								"The purchase price should be a number!",
								"Warning",
								JOptionPane.WARNING_MESSAGE);
						errorPp=true;
					}

					if(errorPp!=true)
					{
						errorSp=false;
						try
						{
							Integer.valueOf(salesP);
						}
						catch(Exception e)
						{
							JOptionPane.showMessageDialog(null,
									"The sales price should be a number!",
									"Warning",
									JOptionPane.WARNING_MESSAGE);
							errorSp=true;
						}

						if(errorSp!=true)
						{	
							errorRp=false;
							try
							{
								Integer.valueOf(rentP);
							}
							catch(Exception e)
							{
								JOptionPane.showMessageDialog(null,
										"The rent price should be a number!",
										"Warning",
										JOptionPane.WARNING_MESSAGE);
								errorRp=true;
							}
							
							if(errorRp!=true)
							{
								if((ctr.getProd(id)))
								{
									JOptionPane.showMessageDialog(null,
											"Supplier with the same name already exists.",
											"Warning",
											JOptionPane.WARNING_MESSAGE);
								}
								else
								{
									ctr.newEquip(id, name, Double.valueOf(purchP), Double.valueOf(salesP), Double.valueOf(rentP), Integer.valueOf(minStock), Integer.valueOf(quantity), cOfO, suppID, type);
									
									//table at customer window.addElement(/*???*/);
								
									dispose();
								
								}
							}
						}
					}
				}
			}
	}	
}